package com.snc.controller.hxsq.school;


import com.snc.controller.index.BaseController;
import com.snc.entity.SchoolFormMap;
import com.snc.exception.SystemException;
import com.snc.plugin.PageView;
import com.snc.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lanyuan 2015-11-19
 * @version 4.0v
 * @Email: mmm333zzz520@163.com
 */
@Controller
@RequestMapping("/school/")
public class SchoolAdminController extends BaseController {

    @RequestMapping("listUI")
    public String listUI(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/hxsq/school/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow,
                               String pageSize) throws Exception {
        pageView = getPageView(pageNow, pageSize);
        int limit = (pageView.getPageNow() - 1) * pageView.getPageSize();
        int offset = pageView.getPageSize();

        SchoolFormMap map = new SchoolFormMap();
        long count = map.findByAll().size();
        pageView.setRowCount(count);
        map.put("limit", limit);
        map.put("offset", offset);
        pageView = map.findByPage(pageView);
        return pageView;
    }

    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/hxsq/school/add";
    }

    @ResponseBody
    @RequestMapping("addEntity")
    @Transactional(readOnly = false)
    public String addEntity() {
        try {
            SchoolFormMap map = super.toFormMap(SchoolFormMap.class);
            map.save();

        } catch (Exception e) {
            //try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
            String results = "{\"results\":\"error\",\"message\":\"" + e + "\"}";
            throw new SystemException(results);
        }
        return "success";
    }

    @RequestMapping("editUI")
    public String editUI(Model model) throws Exception {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            model.addAttribute("school", new SchoolFormMap().findById(id));
        }
        return Common.BACKGROUND_PATH + "/hxsq/school/edit";
    }

    @ResponseBody
    @RequestMapping("editEntity")
    @Transactional(readOnly = false)
    public String editEntity(HttpServletRequest request) throws Exception {
        try {
            SchoolFormMap schoolFormMap = new SchoolFormMap();

            schoolFormMap.put("id", request.getParameter("id"));
            schoolFormMap.put("name", request.getParameter("name"));
            schoolFormMap.put("wd", request.getParameter("wd"));
            schoolFormMap.put("jd", request.getParameter("jd"));

            schoolFormMap.update();

        } catch (Exception e) {
            //try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
            String results = "{\"results\":\"error\",\"message\":\"" + e + "\"}";
            throw new SystemException(results);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly = false)
    public String deleteEntity(String ids) throws Exception {
        for (String id : ids.split(",")) {
            SchoolFormMap map = new SchoolFormMap();
            map.deleteById(id);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("handle")
    @Transactional(readOnly = false)
    public String handle(String ids) throws Exception {
        for (String id : ids.split(",")) {
            SchoolFormMap map = new SchoolFormMap();
            map.put("id", id);
            map.put("status", "Y");
            map.update();
        }
        return "success";
    }


}